import { defineStore } from "pinia";
import { System } from "@/api/interface/index";
import piniaPersistConfig from "@/stores/helper/persist";
import { getDictList } from "@/api/modules/system";

interface Dict {
  dictionaries: { [key: string]: System.ResDictList[] };
}

export const useDictStore = defineStore({
  id: "command-dict",
  state: (): Dict => ({
    dictionaries: {}
  }),
  actions: {
    // setDictionary(name, newDictionary) {
    //   this.dictionaries[name] = newDictionary;
    // },
    async getDictionary(dictType: any) {
      if (dictType in this.dictionaries) {
        return this.dictionaries[dictType];
      }
      let { data } = await getDictList({ dictType });
      this.dictionaries[dictType] = data as System.ResDictList[];
      return this.dictionaries[dictType];
    },
    // getDictionaryNames() {
    //   return Object.keys(this.dictionaries);
    // },
    // addToDictionary(name, key, value) {
    //   if (!this.dictionaries[name]) {
    //     this.dictionaries[name] = {};
    //   }
    //   this.dictionaries[name][key] = value;
    // },
    // removeFromDictionary(name, key) {
    //   delete this.dictionaries[name][key];
    // }
    removeAllDictionary() {
      this.dictionaries = {};
    }
  },
  persist: piniaPersistConfig("command-dict", sessionStorage)
});
